<?php

// http://localhost/crystalmonitoring/index.php?act=log&mac=C81F6612319AB&name=IRAKLI-PC&url=https://mail.google.com/mail/u/0/inbox?compose=14be9f104a3eb49c&cursor=True
// http://localhost/crystalmonitoring/index.php?act=getproperties&mac=C81F6612319AB
//http://localhost/crystalmonitoring/index.php?act=upload&mac=C81F6612319AB&file=@filename

mysql_connect ('192.168.11.10', 'root', 'Gl-1114' );
mysql_select_db('crystal_monitoring');

$action = mysql_real_escape_string($_REQUEST['act']);

$ip         = ip();

$mac        = mysql_real_escape_string($_REQUEST['mac']);
$comp_name  = mysql_real_escape_string($_REQUEST['name']);
$url        = mysql_real_escape_string($_REQUEST['url']);
$cursor     = mysql_real_escape_string($_REQUEST['cursor']);

switch ($action) {
    
    case 'log':
        
        mysql_query("INSERT INTO `monitoring_computer_state_log` (`datatime`, `ip`, `mac`, `computer_name`, `url`, `cursor`)
                                                          VALUES (NOW(), '$ip', '$mac', '$comp_name', '$url', '$cursor');");
        
        echo print_r($_REQUEST);
        
    break;
    
    case 'upload':
        
        if (isset($_POST['mac'])) {
            
            if (!empty($_FILES['file'])) {
                
                if ($_FILES['file']['error'] > 0) {
                    
                    echo "Error: " . $_FILES["file"]["error"] ;
                    
                } else {
                    
                    $blob = mysql_real_escape_string(file_get_contents($_FILES['file']['tmp_name']));
                    mysql_query("INSERT INTO `monitoring_screenshots` (`unixtime`, `mac`, `image`) VALUES (UNIX_TIMESTAMP(NOW()), '$_POST[mac]', '$blob');");
                    
                    echo "Upload Successful!";
                    
                }
                
            } else {
                
                die('File not uploaded.');
                
            }
        };
        
    break;
        
    case 'getproperties':
    
        $res = mysql_query("SELECT 	monitoring_computer_state.mac,
                                    monitoring_computer_state.screenshot_enabled,
                                    monitoring_computer_state.refresh_time
                            FROM 	monitoring_computer_state
                            WHERE 	monitoring_computer_state.mac = '$mac'");
        
        $doc = new DomDocument('1.0');
        
        $root = $doc->createElement('SETTINGS');
        $doc->appendChild($root);
        
        
        while ($row = mysql_fetch_assoc($res)) {
        
            $child = $doc->createElement('MAC');
            $root->appendChild($child);
        
            $value = $doc->createTextNode($row['mac']);
            $child->appendChild($value);
        
            $child = $doc->createElement('SCREENSHOT');
            $root->appendChild($child);
        
            $value = $doc->createTextNode($row['screenshot_enabled']);
            $child->appendChild($value);
        
            $child = $doc->createElement('REFRESH');
            $root->appendChild($child);
        
            $value = $doc->createTextNode($row['refresh_time']);
            $child->appendChild($value);
        
        }
        
        $xml_string = $doc->saveXML();
        
        echo $xml_string;
        
    break;
    
    case 'getimage':
        
        $res = mysql_query("SELECT image FROM `monitoring_screenshots` WHERE `monitoring_screenshots`.mac = '$mac'");
        
        while ($row = mysql_fetch_assoc($res)) {
        
            $image = $row['image'];
        
        }
        
        header('Content-type: image/jpg');
        echo $image;
        
        break;

    case 'errorlog':
        
    break;

    default:
        echo 'no match action!!!';
    break;
    
}

function ip(){

    global $REMOTE_ADDR;
    global $HTTP_X_FORWARDED_FOR, $HTTP_X_FORWARDED, $HTTP_FORWARDED_FOR, $HTTP_FORWARDED;
    global $HTTP_VIA, $HTTP_X_COMING_FROM, $HTTP_COMING_FROM;
    if (empty($REMOTE_ADDR)) {
        if (!empty($_SERVER) && isset($_SERVER['REMOTE_ADDR'])) {
            $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
        }
        else if (!empty($_ENV) && isset($_ENV['REMOTE_ADDR'])) {
            $REMOTE_ADDR = $_ENV['REMOTE_ADDR'];
        }
        else if (@getenv('REMOTE_ADDR')) {
            $REMOTE_ADDR = getenv('REMOTE_ADDR');
        }
    }
    if (empty($HTTP_X_FORWARDED_FOR)) {
        if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $HTTP_X_FORWARDED_FOR = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
            $HTTP_X_FORWARDED_FOR = $_ENV['HTTP_X_FORWARDED_FOR'];
        }
        else if (@getenv('HTTP_X_FORWARDED_FOR')) {
            $HTTP_X_FORWARDED_FOR = getenv('HTTP_X_FORWARDED_FOR');
        }
    }
    if (empty($HTTP_X_FORWARDED)) {
        if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED'])) {
            $HTTP_X_FORWARDED = $_SERVER['HTTP_X_FORWARDED'];
        }
        else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED'])) {
            $HTTP_X_FORWARDED = $_ENV['HTTP_X_FORWARDED'];
        }
        else if (@getenv('HTTP_X_FORWARDED')) {
            $HTTP_X_FORWARDED = getenv('HTTP_X_FORWARDED');
        }
    }
    if (empty($HTTP_FORWARDED_FOR)) {
        if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED_FOR'])) {
            $HTTP_FORWARDED_FOR = $_SERVER['HTTP_FORWARDED_FOR'];
        }
        else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED_FOR'])) {
            $HTTP_FORWARDED_FOR = $_ENV['HTTP_FORWARDED_FOR'];
        }
        else if (@getenv('HTTP_FORWARDED_FOR')) {
            $HTTP_FORWARDED_FOR = getenv('HTTP_FORWARDED_FOR');
        }
    }
    if (empty($HTTP_FORWARDED)) {
        if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED'])) {
            $HTTP_FORWARDED = $_SERVER['HTTP_FORWARDED'];
        }
        else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED'])) {
            $HTTP_FORWARDED = $_ENV['HTTP_FORWARDED'];
        }
        else if (@getenv('HTTP_FORWARDED')) {
            $HTTP_FORWARDED = getenv('HTTP_FORWARDED');
        }
    }
    if (empty($HTTP_VIA)) {
        if (!empty($_SERVER) && isset($_SERVER['HTTP_VIA'])) {
            $HTTP_VIA = $_SERVER['HTTP_VIA'];
        }
        else if (!empty($_ENV) && isset($_ENV['HTTP_VIA'])) {
            $HTTP_VIA = $_ENV['HTTP_VIA'];
        }
        else if (@getenv('HTTP_VIA')) {
            $HTTP_VIA = getenv('HTTP_VIA');
        }
    }
    if (empty($HTTP_X_COMING_FROM)) {
        if (!empty($_SERVER) && isset($_SERVER['HTTP_X_COMING_FROM'])) {
            $HTTP_X_COMING_FROM = $_SERVER['HTTP_X_COMING_FROM'];
        }
        else if (!empty($_ENV) && isset($_ENV['HTTP_X_COMING_FROM'])) {
            $HTTP_X_COMING_FROM = $_ENV['HTTP_X_COMING_FROM'];
        }
        else if (@getenv('HTTP_X_COMING_FROM')) {
            $HTTP_X_COMING_FROM = getenv('HTTP_X_COMING_FROM');
        }
    }
    if (empty($HTTP_COMING_FROM)) {
        if (!empty($_SERVER) && isset($_SERVER['HTTP_COMING_FROM'])) {
            $HTTP_COMING_FROM = $_SERVER['HTTP_COMING_FROM'];
        }
        else if (!empty($_ENV) && isset($_ENV['HTTP_COMING_FROM'])) {
            $HTTP_COMING_FROM = $_ENV['HTTP_COMING_FROM'];
        }
        else if (@getenv('HTTP_COMING_FROM')) {
            $HTTP_COMING_FROM = getenv('HTTP_COMING_FROM');
        }
    }

    if (!empty($REMOTE_ADDR)) {
        $direct_ip = $REMOTE_ADDR;
    }

    $proxy_ip	 = '';
    if (!empty($HTTP_X_FORWARDED_FOR)) {
        $proxy_ip = $HTTP_X_FORWARDED_FOR;
    } else if (!empty($HTTP_X_FORWARDED)) {
        $proxy_ip = $HTTP_X_FORWARDED;
    } else if (!empty($HTTP_FORWARDED_FOR)) {
        $proxy_ip = $HTTP_FORWARDED_FOR;
    } else if (!empty($HTTP_FORWARDED)) {
        $proxy_ip = $HTTP_FORWARDED;
    } else if (!empty($HTTP_VIA)) {
        $proxy_ip = $HTTP_VIA;
    } else if (!empty($HTTP_X_COMING_FROM)) {
        $proxy_ip = $HTTP_X_COMING_FROM;
    } else if (!empty($HTTP_COMING_FROM)) {
        $proxy_ip = $HTTP_COMING_FROM;
    }

    if (empty($proxy_ip)) {
        $ip =  $direct_ip;
    } else {
        $is_ip = preg_match('|^([0-9]{1,3}\.){3,3}[0-9]{1,3}|', $proxy_ip, $regs);
        if ($is_ip && (count($regs) > 0)) {
            $ip =  $regs[0];
        } else {
            $ip =  'unknow';
        }
    }

    return $ip;
}

?>